<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      table {
        width: 600px;
        text-align: center;
      }

      table,
      th,
      td {
        border: 1px solid #ccc;
        border-collapse: collapse;
      }

      caption {
        font-size: 18px;
        margin-bottom: 10px;
        font-weight: 700;
      }

      tr {
        height: 40px;
        cursor: pointer;
      }

      table tr:nth-child(1) {
        background-color: #ddd;
      }

      table tr:not(:first-child):hover {
        background-color: #eee;
      }
    </style>
  </head>
  <body>
    <h2>销售信息</h2>
    <p>将数据渲染到页面中...</p>

    <table>
      <caption>
        销售列表
      </caption>
      <tr>
        <th>名次</th>
        <th>姓名</th>
        <th>年纪</th>
        <th>性别</th>
        <th>销售额</th>
        <th>业绩状态</th>
      </tr>
      <!-- script写到这里 -->
      <script>
        let sales = [
          { name: '小明', age: 18, gender: '男', money: 30, status: 0 },
          { name: '小红', age: 26, gender: '女', money: 99, status: 3 },
          { name: '小刚', age: 19, gender: '男', money: 88, status: 2 },
          { name: '小丽', age: 20, gender: '女', money: 67, status: 1 },
          { name: '晓强', age: 23, gender: '女', money: 77, status: 1 },
          { name: '小张', age: 25, gender: '女', money: 80, status: 2 },
        ]
        // 排序实现1

        //sales.sort(function(a, b) {
        //  return  b.money - a.money
        //})

        // 排序实现2
        for (let i = 0; i < sales.length - 1; i++) {
          for (let j = 0; j < sales.length - 1 - i; j++) {
            if (sales[j].money < sales[j + 1].money) {
              var temp = sales[j]
              sales[j] = sales[j + 1]
              sales[j + 1] = temp
            }
          }
        }

        // 2. 渲染页面
        function load(data) {
          for (let i = 0; i < data.length; i++) {
            document.write(`
                        <tr>
                            <td>${i + 1}</td>
                            <td>${data[i].name}</td>
                            <td>${data[i].age}</td>
                            <td>${data[i].gender}</td>
                            <td>${data[i].money}w</td>
                            <td>${
                              { 0: '不及格', 1: '及格', 2: '良好', 3: '优秀' }[
                                data[i].status
                              ]
                            }</td>
                        </tr>`)
          }
        }

        load(sales)
      </script>
    </table>
  </body>
</html>
